<template>
        <!--输入框-->
        <div style="width: 600px;align-items: center;margin-left: -100%;">
          <el-form
            ref="ruleFormRef"
            :model="ruleForm"
            status-icon
            :rules="rules"
            label-position="top"
            label-width="120px"
            style="width: 300px;padding-left: 50%;padding-top: 50;"
          >
          <!-- 输入手机号 -->
            <el-form-item label="手机号：" prop="phone">
              <el-input v-model.number="ruleForm.phone" type="text" autocomplete="off" />
            </el-form-item>
            <!-- 输入密码 -->
            <el-form-item label="密码：" prop="pass">
              <el-input v-model="ruleForm.pass" type="password" show-password autocomplete="off"/>
            </el-form-item>
            <!-- 确认密码 -->
            <el-form-item label="确认密码：" prop="checkPass">
              <el-input v-model="ruleForm.checkPass"  type="password" show-password autocomplete="off"/>
            </el-form-item>
            <!-- 提交表单 -->
            <el-form-item>
              <el-button type="primary" @click="submitForm(ruleForm)">激活</el-button>
              <el-button style="margin-left: 60%;" @click="resetForm">重置</el-button>
              <Vcode :show="isShow" @success="success" @close="close" @fail="fail" :img="[Img]"></Vcode>
            </el-form-item>
          </el-form>
        </div>
    
    
</template>

<script  >
import { ref, reactive } from 'vue';
import { registerpass } from "@/api/index/register"
import Vcode from 'vue3-puzzle-vcode'
import _ from 'lodash'

export default {
  name: 'Register_confirm',
  components: { Vcode },
  setup() {
    // 定义响应式数据模型
    const ruleFormRef = ref(null);
    const ruleForm = reactive({
      pass: '',
      checkPass: '',
      phone: ''
    });
    // 手机号验证
    const validatephone = (rule, value, callback) => {
      const reg = /^1[3-9]\d{9}$/; // 使用正则表达式定义手机号的格式要求

      if (value === '') {
        callback(new Error('请输入手机号'));
      } else if (!reg.test(value)) {
        callback(new Error('手机号格式不正确'));
      } else {
        callback(); // 如果验证通过，调用 callback() 即可
      }
    };
    // 密码验证函数
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        // 若确认密码非空，则验证其与密码是否一致
        if (ruleForm.checkPass !== '') {
          ruleFormRef.value.validateField('checkPass', () => null);
        }
        callback();
      }
    };

    // 确认密码一致性验证函数
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== ruleForm.pass) {
        callback(new Error('两次输入不一致'));
      } else {
        callback();
      }
    };

    // 规则
    const rules = {
      pass: [
        { required: true,validator: validatePass, trigger: 'blur' }
      ],
      checkPass: [
        { required: true,validator: validatePass2, trigger: 'blur' }
      ],
      phone: [
        { required: true, validator:validatephone, trigger: 'blur' }
      ]
    };


    return {
      ruleFormRef,
      ruleForm,
      rules,
      isShow:ref(false)
    };
  },
  // 函数
  methods: {
    // 重置表单
    resetForm() {
    this.$refs.ruleFormRef.resetFields();
  },
  // 提交表单
  submitForm:_.debounce(function(){
    this.$refs.ruleFormRef.validate((valid) => {
      if (valid) {
        // 表单验证通过，可以在这里执行提交操作，比如发送表单数据到后端
        this.Show()
        console.log('表单验证通过，提交的数据为：', this.ruleForm);
        // 在这里可以编写发送表单数据的代码
      } else {
        // 表单验证不通过，可以进行错误处理或者给出提示
        console.log('表单验证未通过，请检查输入');
      }
    });

  },500),
  // 提交密码请求
  activation() {
    registerpass({
      phone: this.ruleForm.phone,
      password: this.ruleForm.pass,
    }).then((resp) =>{
      let code=resp.data.code;
      if(code===200){
        // var path = this.$route.query.redirect
        // this.$router.replace({path: path === '/register4' || path === undefined ? '/register4' : path})
        this.$emit('nextPage')
        }else{
          alert('不存在该用户或者用户名和身份证号不匹配');
        }
    });
  },
  // 验证码逻辑
  Show(){
      console.log('开始验证')
      this.isShow = true

    },
  success (msg){
    this.isShow= false
    console.log('验证通过' + msg)
    //通过验证向后端传送数据
    this.activation()
  },
  //用户点击遮罩层，关闭模态框
   close(){
    this.isShow = false
  },
  //用户验证失败
   fail(){
    console.log('验证失败')
  }

  }
};
</script>

<style scoped>

</style>
